
var base_context = '';
var base_url =  base_context + '/rest';

/**
 * 获取url参数
 * @param {String} name 
 * @returns 
 */
window.getQueryString = function(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
    var r = window.location.search.substr(1).match(reg);
    if (r != null) return unescape(r[2]);
    return null;
}

window.exportData = function (data, fields, name) {
    //用field字段名称替换data的字段名称
    for(var i in data){
        var item = data[i];
        for(var j in item){
            if(fields.hasOwnProperty(j)){
                item[fields[j]] = item[j];
            }
            delete item[j];
        }
    }
    var sheet = XLSX.utils.json_to_sheet(data, {header:Object.values(fields)}) //将JS对象数组转换为工作表。
    openDownloadDialog(sheet2blob(sheet), name);
}

window.sheet2blob = function (sheet, sheetName) {
    sheetName = sheetName || 'sheet1';
    var workbook = {
        SheetNames: [sheetName],
        Sheets: {}
    };
    workbook.Sheets[sheetName] = sheet; // 生成excel的配置项

    var wopts = {
        bookType: 'xlsx', // 要生成的文件类型
        bookSST: false, // 是否生成Shared String Table，官方解释是，如果开启生成速度会下降，但在低版本IOS设备上有更好的兼容性
        type: 'binary'
    };
    var wbout = XLSX.write(workbook, wopts);
    var blob = new Blob([s2ab(wbout)], {
        type: "application/octet-stream"
    }); // 字符串转ArrayBuffer
    function s2ab(s) {
        var buf = new ArrayBuffer(s.length);
        var view = new Uint8Array(buf);
        for (var i = 0; i != s.length; ++i) view[i] = s.charCodeAt(i) & 0xFF;
        return buf;
    }
    return blob;
}

window.openDownloadDialog = function (blob, fileName) {
    var url;
    if (typeof blob == 'object' && blob instanceof Blob) {
        url = URL.createObjectURL(blob); // 创建blob地址
    }
    var aLink = document.createElement('a');
    aLink.href = url;
    aLink.download = fileName || ''; // HTML5新增的属性，指定保存文件名，可以不要后缀，注意，file:///模式下不会生效
    var event;
    if (window.MouseEvent) event = new MouseEvent('click');
    else {
        event = document.createEvent('MouseEvents');
        event.initMouseEvent('click', true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
    }
    aLink.dispatchEvent(event);
}



layui.$.put = function (params) {
    Object.assign( params,
        {
            type: "put",
            contentType: "application/json",
            data: JSON.stringify(params.data),
            dataType: "json",
            async: false,
            processData: false
        });

    layui.$.ajax(params);
}

layui.$.postContent = function (params) {
    Object.assign( params,
        {
            type: "post",
            contentType: "application/json",
            data: JSON.stringify(params.data),
            dataType: "json",
            async: false,
            processData: false
        });

    layui.$.ajax(params);
}

layui.$.delete = function (params) {
    Object.assign( params,
        {
            type: "delete",
            contentType: "application/json",
            data: JSON.stringify(params.data),
            dataType: "json",
            async: false,
            processData: false
        });

    layui.$.ajax(params);
}

layui.$.download = function (params) {
    var xhr = new XMLHttpRequest();
    xhr.open("POST", params.url, true);
    xhr.responseType = "blob";
    xhr.setRequestHeader("Content-type","application/json");
    xhr.onload = function () {
        if (this.status == 200) {
            var blob = this.response;
            var reader = new FileReader();
            reader.readAsDataURL(blob);
            reader.onload = function (e) {
                var a = document.createElement('a');
                a.download = params.fileName;
                a.href = e.target.result;
                document.body.appendChild(a);
                a.click();
                document.body.removeChild(a);
            }
        }
    };
    xhr.send(JSON.stringify(params.data));
}



function initVue(params) {

    var options = {
        el: '#app',
        data: window.data || {},
        created: typeof window.created == "function" ? window.created : function () {},
        updated: typeof window.updated == "function" ? window.updated : function () {},
        methods: window.methods || {}
    }

    Object.assign(options, params);

    // layui.$(options.el).hide(); 在元素上设置display:none效果才最好
    // 可以优化为渲染完毕后再显示
    setTimeout(function () {
        layui.$(options.el).show();
    }, 1000);

    console.log(options);

    return new Vue(options);

}